package com.teng.mylibrary.base

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.viewbinding.ViewBinding
import com.teng.mylibrary.R

/**
 * Description:  基类弹窗
 * Author:       liuteng
 * CreateDate:   2021/9/9 11:07
 * UpdateRemark: 更新说明
 */
abstract class BaseDialog<V : ViewBinding>(private val inflater: (LayoutInflater, ViewGroup?, Boolean) -> V) :
    DialogFragment() {

    lateinit var binding: V
    protected var mActivity: Activity? = null

    abstract fun initView(view: View?)

    abstract fun initData()

    protected var mDismissListener: ((Any?) -> Any)? = null
    protected var mOnConfirmClickListener: (() -> Any)? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as AppCompatActivity
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //设置背景为透明
        dialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        //点击Dialog之外的地方，不能取消dialog
        dialog!!.setCanceledOnTouchOutside(false)
        //去掉Dialog的标题
        dialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)

        binding = inflater(layoutInflater, container, false)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        dialog?.window?.setGravity(Gravity.CENTER)
        dialog?.window?.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        //动画
        dialog?.window?.setWindowAnimations(R.style.main_menu_animStyle)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initView(view)
        initData()
    }

    open fun setOnDismissListener(@Nullable listener: ((Any?) -> Any)) {
        this.mDismissListener = listener
    }


    fun setOnConfirmClickListener(@Nullable onClickListener: (() -> Any)) {
        mOnConfirmClickListener = onClickListener
    }

}